//
//  RadarView.m
//  CTLMapAnnotationView
//
//  Created by DWE_KJ on 2017/1/9.
//  Copyright © 2017年 CTL. All rights reserved.
//

#import "RadarView.h"

@implementation RadarView

+ (instancetype)shared
{
    static RadarView *util = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        util = [[self alloc]init];
    });
    return util;
}

- (void)hideActivity:(UIView *)view
{
    [view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if([obj isKindOfClass:[MBProgressHUD class]])
        {
            MBProgressHUD *hud = (MBProgressHUD *)obj;
            dispatch_async(dispatch_get_main_queue(), ^{
                [hud hideAnimated:YES];
            });
        }
    }];
}

- (void)showRadarView:(UIView *)view
{
    [self hideActivity:view];
    if (!view) return;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.bezelView.color = [UIColor clearColor];
    hud.backgroundView.color = [UIColor clearColor];
    hud.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.mode = MBProgressHUDModeCustomView;
    hud.removeFromSuperViewOnHide = YES;
    
    hud.bezelView.bounds = hud.bounds;
    hud.backgroundView.bounds = hud.bounds;
    
    hud.customView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"scanPartCircle"]];
    [hud.customView setContentMode:UIViewContentModeCenter];
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.fromValue = @0.0f;
    rotationAnimation.toValue = @(2 * M_PI);
    rotationAnimation.speed = 0.5;
    rotationAnimation.duration = 1.0;
    rotationAnimation.repeatCount = HUGE_VALF;
    [hud.customView.layer addAnimation:rotationAnimation forKey:@"radarAnimation"];
    
}

@end
